package com.mystnihon.freeremote.ui.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;

import com.actionbarsherlock.app.SherlockFragment;
import com.mystnihon.freeremote.FreeRemoteApplication;
import com.mystnihon.freeremote.R;
import com.mystnihon.freeremote.loader.remote.Remote;
import com.mystnihon.freeremote.ui.AddTorrentFileActivity;
import com.mystnihon.freeremote.util.SettingsLauncher;
import com.mystnihon.widget.RotaryKnobView;

public class RemoteFragment extends SherlockFragment {

	private ImageButton mButtonUp;
	private ImageButton mButtonDown;
	private ImageButton mButtonLeft;
	private ImageButton mButtonRight;
	private RotaryKnobView mRotaryKnobView;
	private ImageButton mProgramUp;
	private ImageButton mProgramDown;
	private ImageButton mButtonMenu;
	private ImageButton mButtonSearch;
	private ImageButton mButtonInfo;
	private ImageButton mButtonBack;
	private Button mButtonOk;
	private ImageButton mButtonPower;
	private ImageButton mFree;
	private ImageButton mRewind;
	private ImageButton mPlay;
	private ImageButton mForward;

	private SettingsLauncher mJamaPart;
	private Remote mRemote;

	public static RemoteFragment newInstance() {
		RemoteFragment f = new RemoteFragment();
		return f;

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment_general_controls, null);
		mButtonUp = (ImageButton) v.findViewById(R.id.fr_btn_up);
		mButtonDown = (ImageButton) v.findViewById(R.id.fr_btn_down);
		mButtonLeft = (ImageButton) v.findViewById(R.id.fr_btn_left);
		mButtonRight = (ImageButton) v.findViewById(R.id.fr_btn_right);
		mRotaryKnobView = (RotaryKnobView) v.findViewById(R.id.fr_volume);
		mButtonMenu = (ImageButton) v.findViewById(R.id.fr_btn_menu);
		mButtonSearch = (ImageButton) v.findViewById(R.id.fr_btn_search);
		mButtonInfo = (ImageButton) v.findViewById(R.id.fr_btn_info);
		mButtonBack = (ImageButton) v.findViewById(R.id.fr_btn_back);
		mButtonOk = (Button) v.findViewById(R.id.fr_btn_ok);
		mProgramUp = (ImageButton) v.findViewById(R.id.fr_btn_pgr_up);
		mProgramDown = (ImageButton) v.findViewById(R.id.fr_btn_pgr_down);
		mFree = (ImageButton) v.findViewById(R.id.fr_btn_free_logo_key);
		mRewind = (ImageButton) v.findViewById(R.id.fr_btn_rewind);
		mPlay = (ImageButton) v.findViewById(R.id.fr_btn_play);
		mForward = (ImageButton) v.findViewById(R.id.fr_btn_forward);
		mButtonPower = (ImageButton) v.findViewById(R.id.fr_btn_power);

		mButtonUp.setOnClickListener(mControlsListener);
		mButtonDown.setOnClickListener(mControlsListener);
		mButtonLeft.setOnClickListener(mControlsListener);
		mButtonRight.setOnClickListener(mControlsListener);

		mButtonMenu.setOnClickListener(mControlsListener);
		mButtonSearch.setOnClickListener(mControlsListener);
		mButtonInfo.setOnClickListener(mControlsListener);
		mButtonBack.setOnClickListener(mControlsListener);
		mButtonOk.setOnClickListener(mControlsListener);

		mProgramUp.setOnClickListener(mControlsListener);
		mProgramDown.setOnClickListener(mControlsListener);
		mButtonPower.setOnClickListener(mControlsListener);

		mFree.setOnClickListener(mControlsListener);
		mRewind.setOnClickListener(mControlsListener);
		mPlay.setOnClickListener(mControlsListener);
		mForward.setOnClickListener(mControlsListener);

		((Button) v.findViewById(R.id.fr_btn_key_0))
				.setOnClickListener(mControlsListener);
		((Button) v.findViewById(R.id.fr_btn_key_1))
				.setOnClickListener(mControlsListener);
		((Button) v.findViewById(R.id.fr_btn_key_2))
				.setOnClickListener(mControlsListener);
		((Button) v.findViewById(R.id.fr_btn_key_3))
				.setOnClickListener(mControlsListener);
		((Button) v.findViewById(R.id.fr_btn_key_4))
				.setOnClickListener(mControlsListener);
		((Button) v.findViewById(R.id.fr_btn_key_5))
				.setOnClickListener(mControlsListener);
		((Button) v.findViewById(R.id.fr_btn_key_6))
				.setOnClickListener(mControlsListener);
		((Button) v.findViewById(R.id.fr_btn_key_7))
				.setOnClickListener(mControlsListener);
		((Button) v.findViewById(R.id.fr_btn_key_8))
				.setOnClickListener(mControlsListener);
		((Button) v.findViewById(R.id.fr_btn_key_9))
				.setOnClickListener(mControlsListener);
		return v;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		FreeRemoteApplication app = (FreeRemoteApplication) getSherlockActivity()
				.getApplication();
		mRemote = app.getRemote();
		mJamaPart = new SettingsLauncher(getActivity());
		mRotaryKnobView
				.setKnobListener(new RotaryKnobView.RotaryKnobListener() {

					@Override
					public void onKnobChanged(int direction) {
						if (direction > 0) {
							mRemote.increaseVolume(1);
						} else if (direction < 0) {
							mRemote.decreaseVolume(1);
						}

					}
				});

		if (!initRemote()) {
			askToDisplaySettings();
		}

	}

	@Override
	public void onResume() {
		super.onResume();

		initRemote();
	}

	protected void openSettingsScreen() {
		mJamaPart.openSettingsScreen();
	}

	private boolean initRemote() {
		String remoteCode = PreferenceManager.getDefaultSharedPreferences(
				getSherlockActivity()).getString(
				getString(R.string.pref_key_remote_code), null);

		if (remoteCode != null && remoteCode.length() > 0) {
			mRemote.setRemoteCode(remoteCode);
			return true;
		} else {
			return false;
		}
	}

	private void askToDisplaySettings() {
		AlertDialog.Builder builder = new AlertDialog.Builder(
				getSherlockActivity());
		builder.setTitle(R.string.check_settings_title)
				.setMessage(R.string.check_settings_message)
				.setOnCancelListener(new DialogInterface.OnCancelListener() {

					@Override
					public void onCancel(DialogInterface dialog) {
						dialog.dismiss();

					}
				})
				.setPositiveButton(R.string.yes,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								openSettingsScreen();

							}
						})
				.setNegativeButton(R.string.no,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.cancel();

							}
						}).show();
	}

	private View.OnClickListener mControlsListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.fr_btn_down:
				mRemote.keyDown(-1);
				break;
			case R.id.fr_btn_up:
				mRemote.keyUp(-1);
				break;
			case R.id.fr_btn_left:
				mRemote.keyLeft(-1);
				break;
			case R.id.fr_btn_right:
				mRemote.keyRight(-1);
				break;
			case R.id.fr_btn_menu:
				mRemote.keyMenu(-1);
				break;
			case R.id.fr_btn_info:
				mRemote.keyInfo(-1);
				break;
			case R.id.fr_btn_search:
				mRemote.keySearch(-1);
				break;
			case R.id.fr_btn_back:
				mRemote.keyBack(-1);
				break;
			case R.id.fr_btn_pgr_up:
				mRemote.nextChannel(-1);
				break;
			case R.id.fr_btn_pgr_down:
				mRemote.prevChannel(-1);
				break;
			case R.id.fr_btn_ok:
				mRemote.keyOk();
				break;
			case R.id.fr_btn_key_0:
				mRemote.keyNumber(0);
				break;
			case R.id.fr_btn_key_1:
				mRemote.keyNumber(1);
				break;
			case R.id.fr_btn_key_2:
				mRemote.keyNumber(2);
				break;
			case R.id.fr_btn_key_3:
				mRemote.keyNumber(3);
				break;
			case R.id.fr_btn_key_4:
				mRemote.keyNumber(4);
				break;
			case R.id.fr_btn_key_5:
				mRemote.keyNumber(5);
				break;
			case R.id.fr_btn_key_6:
				mRemote.keyNumber(6);
				break;
			case R.id.fr_btn_key_7:
				mRemote.keyNumber(7);
				break;
			case R.id.fr_btn_key_8:
				mRemote.keyNumber(8);
				break;
			case R.id.fr_btn_key_9:
				mRemote.keyNumber(9);
				break;
			case R.id.fr_btn_power:
				mRemote.keyPower(-1);
				break;
			case R.id.fr_btn_free_logo_key:
				mRemote.keyHome();
				break;
			case R.id.fr_btn_rewind:
				mRemote.keyRwd();
				break;
			case R.id.fr_btn_forward:
				mRemote.keyFwd();
				break;
			case R.id.fr_btn_play:
				// FIXME Test case here. Remove it when done.
				// mRemote.keyPlayPause();
				startActivity(new Intent(getActivity(), AddTorrentFileActivity.class));

				break;
			default:
				break;
			}

		}
	};
}
